********************************************************************************
**  TITLE:      on2003_oes_contact_networks                                   **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       Ontario Election Study, 2003                                  **
**  DATE:	    October 2022 						                          **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "on2003_oes_contact_networks", replace

* Open dataset

use "oes-E-2003_F1.dta", clear


***********************
** DISTRICT OUTCOMES **
***********************

gen district_code = .

replace district_code = 1 if ridin == 93
replace district_code = 95 if ridin == 70
replace district_code = 2 if ridin == 85
replace district_code = 3 if ridin == 1
replace district_code = 4 if ridin == 23
replace district_code = 5 if ridin == 24
replace district_code = 6 if ridin == 25
replace district_code = 7 if ridin == 55
replace district_code = 9 if ridin == 86
replace district_code = 10 if ridin == 26
replace district_code = 11 if ridin == 56
replace district_code = 35 if ridin == 45
replace district_code = 12 if ridin == 12
replace district_code = 13 if ridin == 3
replace district_code = 14 if ridin == 4
replace district_code = 15 if ridin == 87
replace district_code = 16 if ridin == 27
replace district_code = 17 if ridin == 13
replace district_code = 18 if ridin == 42
replace district_code = 19 if ridin == 57
replace district_code = 20 if ridin == 43
replace district_code = 21 if ridin == 14
replace district_code = 23 if ridin == 16
replace district_code = 22 if ridin == 15
replace district_code = 24 if ridin == 71
replace district_code = 26 if ridin == 58
replace district_code = 27 if ridin == 59
replace district_code = 93 if ridin == 92
replace district_code = 28 if ridin == 28
replace district_code = 29 if ridin == 60
replace district_code = 30 if ridin == 61
replace district_code = 31 if ridin == 62
replace district_code = 32 if ridin == 73
replace district_code = 33 if ridin == 44
replace district_code = 34 if ridin == 94
replace district_code = 36 if ridin == 74
replace district_code = 37 if ridin == 63
replace district_code = 38 if ridin == 64
replace district_code = 39 if ridin == 46
replace district_code = 40 if ridin == 75
replace district_code = 41 if ridin == 76
replace district_code = 42 if ridin == 47
replace district_code = 44 if ridin == 49
replace district_code = 43 if ridin == 48
replace district_code = 45 if ridin == 29
replace district_code = 46 if ridin == 30
replace district_code = 47 if ridin == 31
replace district_code = 48 if ridin == 32
replace district_code = 49 if ridin == 33
replace district_code = 50 if ridin == 77
replace district_code = 51 if ridin == 65
replace district_code = 52 if ridin == 66
replace district_code = 53 if ridin == 95
replace district_code = 54 if ridin == 96
replace district_code = 55 if ridin == 88
replace district_code = 56 if ridin == 34
replace district_code = 57 if ridin == 35
replace district_code = 58 if ridin == 36
replace district_code = 59 if ridin == 78
replace district_code = 60 if ridin == 79
replace district_code = 62 if ridin == 81
replace district_code = 25 if ridin == 72
replace district_code = 61 if ridin == 80
replace district_code = 63 if ridin == 50
replace district_code = 64 if ridin == 17
replace district_code = 65 if ridin == 97
replace district_code = 66 if ridin == 51
replace district_code = 67 if ridin == 82
replace district_code = 68 if ridin == 37
replace district_code = 69 if ridin == 89
replace district_code = 70 if ridin == 83
replace district_code = 71 if ridin == 52
replace district_code = 72 if ridin == 98
replace district_code = 74 if ridin == 6
replace district_code = 75 if ridin == 7
replace district_code = 77 if ridin == 9
replace district_code = 73 if ridin == 5
replace district_code = 76 if ridin == 8
replace district_code = 79 if ridin == 91
replace district_code = 78 if ridin == 90
replace district_code = 80 if ridin == 67
replace district_code = 81 if ridin == 18
replace district_code = 82 if ridin == 68
replace district_code = 83 if ridin == 84
replace district_code = 84 if ridin == 99
replace district_code = 85 if ridin == 38
replace district_code = 86 if ridin == 100
replace district_code = 87 if ridin == 101
replace district_code = 88 if ridin == 102
replace district_code = 89 if ridin == 103
replace district_code = 90 if ridin == 10
replace district_code = 8 if ridin == 2
replace district_code = 91 if ridin == 19
replace district_code = 92 if ridin == 39
replace district_code = 94 if ridin == 69
replace district_code = 96 if ridin == 40
replace district_code = 97 if ridin == 11
replace district_code = 99 if ridin == 54
replace district_code = 98 if ridin == 53
replace district_code = 100 if ridin == 20
replace district_code = 101 if ridin == 41
replace district_code = 102 if ridin == 21
replace district_code = 103 if ridin == 22

* Election outcome in each district (1 = OPC, 2 = OLP, 3 = ONDP, 4 = GPO, 5 = other)

gen district_outcome = .

replace district_outcome =  1 if district_code == 1
replace district_outcome =  2 if district_code == 2
replace district_outcome =  3 if district_code == 3
replace district_outcome =  1 if district_code == 4
replace district_outcome =  1 if district_code == 5
replace district_outcome =  1 if district_code == 6
replace district_outcome =  1 if district_code == 7
replace district_outcome =  3 if district_code == 8
replace district_outcome =  2 if district_code == 9
replace district_outcome =  2 if district_code == 10
replace district_outcome =  2 if district_code == 11
replace district_outcome =  1 if district_code == 12
replace district_outcome =  1 if district_code == 13
replace district_outcome =  1 if district_code == 14
replace district_outcome =  2 if district_code == 15
replace district_outcome =  2 if district_code == 16
replace district_outcome =  1 if district_code == 17
replace district_outcome =  1 if district_code == 18
replace district_outcome =  2 if district_code == 19
replace district_outcome =  1 if district_code == 20
replace district_outcome =  1 if district_code == 21
replace district_outcome =  1 if district_code == 22
replace district_outcome =  1 if district_code == 23
replace district_outcome =  1 if district_code == 24
replace district_outcome =  1 if district_code == 25
replace district_outcome =  1 if district_code == 26
replace district_outcome =  2 if district_code == 27
replace district_outcome =  2 if district_code == 28
replace district_outcome =  1 if district_code == 29
replace district_outcome =  1 if district_code == 30
replace district_outcome =  1 if district_code == 31
replace district_outcome =  1 if district_code == 32
replace district_outcome =  1 if district_code == 33
replace district_outcome =  3 if district_code == 34
replace district_outcome =  1 if district_code == 35
replace district_outcome =  1 if district_code == 36
replace district_outcome =  1 if district_code == 37
replace district_outcome =  2 if district_code == 38
replace district_outcome =  1 if district_code == 39
replace district_outcome =  2 if district_code == 40
replace district_outcome =  2 if district_code == 41
replace district_outcome =  1 if district_code == 42
replace district_outcome =  1 if district_code == 43
replace district_outcome =  1 if district_code == 44
replace district_outcome =  1 if district_code == 45
replace district_outcome =  1 if district_code == 46
replace district_outcome =  1 if district_code == 47
replace district_outcome =  1 if district_code == 48
replace district_outcome =  1 if district_code == 49
replace district_outcome =  2 if district_code == 50
replace district_outcome =  3 if district_code == 51
replace district_outcome =  1 if district_code == 52
replace district_outcome =  3 if district_code == 53
replace district_outcome =  1 if district_code == 54
replace district_outcome =  1 if district_code == 55
replace district_outcome =  2 if district_code == 56
replace district_outcome =  1 if district_code == 57
replace district_outcome =  2 if district_code == 58
replace district_outcome =  1 if district_code == 59
replace district_outcome =  1 if district_code == 60
replace district_outcome =  1 if district_code == 61
replace district_outcome =  1 if district_code == 62
replace district_outcome =  2 if district_code == 63
replace district_outcome =  1 if district_code == 64
replace district_outcome =  2 if district_code == 65
replace district_outcome =  1 if district_code == 66
replace district_outcome =  1 if district_code == 67
replace district_outcome =  1 if district_code == 68
replace district_outcome =  1 if district_code == 69
replace district_outcome =  2 if district_code == 70
replace district_outcome =  1 if district_code == 71
replace district_outcome =  1 if district_code == 72
replace district_outcome =  1 if district_code == 73
replace district_outcome =  1 if district_code == 74
replace district_outcome =  1 if district_code == 75
replace district_outcome =  1 if district_code == 76
replace district_outcome =  1 if district_code == 77
replace district_outcome =  2 if district_code == 78
replace district_outcome =  2 if district_code == 79
replace district_outcome =  1 if district_code == 80
replace district_outcome =  1 if district_code == 81
replace district_outcome =  1 if district_code == 82
replace district_outcome =  1 if district_code == 83
replace district_outcome =  1 if district_code == 84
replace district_outcome =  1 if district_code == 85
replace district_outcome =  1 if district_code == 86
replace district_outcome =  1 if district_code == 87
replace district_outcome =  1 if district_code == 88
replace district_outcome =  3 if district_code == 89
replace district_outcome =  1 if district_code == 90
replace district_outcome =  3 if district_code == 91
replace district_outcome =  1 if district_code == 92
replace district_outcome =  2 if district_code == 93
replace district_outcome =  2 if district_code == 94
replace district_outcome =  1 if district_code == 95
replace district_outcome =  2 if district_code == 96
replace district_outcome =  1 if district_code == 97
replace district_outcome =  1 if district_code == 98
replace district_outcome =  1 if district_code == 99
replace district_outcome =  1 if district_code == 100
replace district_outcome =  2 if district_code == 101
replace district_outcome =  1 if district_code == 102
replace district_outcome =  1 if district_code == 103


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 16.96785 if district_code == 1
replace margin = 15.65359 if district_code == 2
replace margin = 26.93857 if district_code == 3
replace margin = 8.874874 if district_code == 4
replace margin = 2.622654 if district_code == 5
replace margin = 4.01009 if district_code == 6
replace margin = 23.90096 if district_code == 7
replace margin = 15.51435 if district_code == 8
replace margin = 18.86923 if district_code == 9
replace margin = 3.974163 if district_code == 10
replace margin = 7.304684 if district_code == 11
replace margin = 31.47945 if district_code == 12
replace margin = 24.76764 if district_code == 13
replace margin = 13.64502 if district_code == 14
replace margin = 27.83959 if district_code == 15
replace margin = 10.33064 if district_code == 16
replace margin = 27.17579 if district_code == 17
replace margin = 27.06464 if district_code == 18
replace margin = 9.669498 if district_code == 19
replace margin = 17.49807 if district_code == 20
replace margin = 9.985672 if district_code == 21
replace margin = 11.56858 if district_code == 22
replace margin = 31.45955 if district_code == 23
replace margin = 41.08949 if district_code == 24
replace margin = 9.03228 if district_code == 25
replace margin = 5.136089 if district_code == 26
replace margin = 6.781918 if district_code == 27
replace margin = 7.885036 if district_code == 28
replace margin = 22.73023 if district_code == 29
replace margin = 25.33466 if district_code == 30
replace margin = 5.462048 if district_code == 31
replace margin = 18.87688 if district_code == 32
replace margin = 7.567554 if district_code == 33
replace margin = 34.2287 if district_code == 34
replace margin = 29.43554 if district_code == 35
replace margin = 40.15824 if district_code == 36
replace margin = 5.033909 if district_code == 37
replace margin = 2.69891 if district_code == 38
replace margin = 8.453823 if district_code == 39
replace margin = 10.20661 if district_code == 40
replace margin = 8.576553 if district_code == 41
replace margin = 14.50795 if district_code == 42
replace margin = 4.815893 if district_code == 43
replace margin = 20.35323 if district_code == 44
replace margin = 11.37545 if district_code == 45
replace margin = 6.732967 if district_code == 46
replace margin = 8.326283 if district_code == 47
replace margin = .5954514 if district_code == 48
replace margin = 13.65847 if district_code == 49
replace margin = 18.41278 if district_code == 50
replace margin = 22.93954 if district_code == 51
replace margin = 8.802025 if district_code == 52
replace margin = 7.885647 if district_code == 53
replace margin = 8.374401 if district_code == 54
replace margin = 5.671093 if district_code == 55
replace margin = 3.650551 if district_code == 56
replace margin = 7.63303 if district_code == 57
replace margin = 2.610611 if district_code == 58
replace margin = 22.11681 if district_code == 59
replace margin = 17.27328 if district_code == 60
replace margin = 27.28655 if district_code == 61
replace margin = 5.796803 if district_code == 62
replace margin = 5.953339 if district_code == 63
replace margin = 41.65284 if district_code == 64
replace margin = 14.06464 if district_code == 65
replace margin = 3.355759 if district_code == 66
replace margin = 11.27969 if district_code == 67
replace margin = 1.850967 if district_code == 68
replace margin = 25.36088 if district_code == 69
replace margin = 1.477123 if district_code == 70
replace margin = 16.5442 if district_code == 71
replace margin = 24.66856 if district_code == 72
replace margin = 31.01518 if district_code == 73
replace margin = 24.02457 if district_code == 74
replace margin = 17.66138 if district_code == 75
replace margin = 38.63443 if district_code == 76
replace margin = 15.21856 if district_code == 77
replace margin = 16.96755 if district_code == 78
replace margin = 7.256237 if district_code == 79
replace margin = 28.10373 if district_code == 80
replace margin = 30.10781 if district_code == 81
replace margin = 10.34694 if district_code == 82
replace margin = 14.68163 if district_code == 83
replace margin = 54.78758 if district_code == 84
replace margin = 1.743053 if district_code == 85
replace margin = 36.63448 if district_code == 86
replace margin = 57.43065 if district_code == 87
replace margin = 39.18027 if district_code == 88
replace margin = 8.542915 if district_code == 89
replace margin = 30.73862 if district_code == 90
replace margin = 15.63594 if district_code == 91
replace margin = 23.08265 if district_code == 92
replace margin = 13.90602 if district_code == 93
replace margin = 11.30633 if district_code == 94
replace margin = 10.11509 if district_code == 95
replace margin = 8.244774 if district_code == 96
replace margin = 4.015839 if district_code == 97
replace margin = 25.82346 if district_code == 98
replace margin = 41.52692 if district_code == 99
replace margin = 34.5736 if district_code == 100
replace margin = 6.665897 if district_code == 101
replace margin = 42.26636 if district_code == 102
replace margin = 52.29219 if district_code == 103

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.716707 if district_code == 1
replace enep = 2.452456 if district_code == 2
replace enep = 2.759179 if district_code == 3
replace enep = 2.794901 if district_code == 4
replace enep = 2.685492 if district_code == 5
replace enep = 2.511468 if district_code == 6
replace enep = 2.461937 if district_code == 7
replace enep = 2.838674 if district_code == 8
replace enep = 2.557221 if district_code == 9
replace enep = 2.5108 if district_code == 10
replace enep = 2.957849 if district_code == 11
replace enep = 2.338826 if district_code == 12
replace enep = 2.313715 if district_code == 13
replace enep = 2.313325 if district_code == 14
replace enep = 2.428453 if district_code == 15
replace enep = 2.680543 if district_code == 16
replace enep = 2.359842 if district_code == 17
replace enep = 2.329971 if district_code == 18
replace enep = 2.530648 if district_code == 19
replace enep = 2.907842 if district_code == 20
replace enep = 2.458962 if district_code == 21
replace enep = 2.924742 if district_code == 22
replace enep = 2.769994 if district_code == 23
replace enep = 1.993584 if district_code == 24
replace enep = 2.335474 if district_code == 25
replace enep = 2.981038 if district_code == 26
replace enep = 2.63378 if district_code == 27
replace enep = 2.488067 if district_code == 28
replace enep = 2.655738 if district_code == 29
replace enep = 2.668143 if district_code == 30
replace enep = 3.090892 if district_code == 31
replace enep = 2.563178 if district_code == 32
replace enep = 2.703625 if district_code == 33
replace enep = 2.24726 if district_code == 34
replace enep = 2.240001 if district_code == 35
replace enep = 2.371545 if district_code == 36
replace enep = 2.863511 if district_code == 37
replace enep = 2.763225 if district_code == 38
replace enep = 2.84257 if district_code == 39
replace enep = 2.524184 if district_code == 40
replace enep = 2.479761 if district_code == 41
replace enep = 3.004832 if district_code == 42
replace enep = 3.149228 if district_code == 43
replace enep = 2.603664 if district_code == 44
replace enep = 2.309651 if district_code == 45
replace enep = 2.509004 if district_code == 46
replace enep = 2.465561 if district_code == 47
replace enep = 2.579697 if district_code == 48
replace enep = 2.478626 if district_code == 49
replace enep = 2.352897 if district_code == 50
replace enep = 2.728883 if district_code == 51
replace enep = 2.629501 if district_code == 52
replace enep = 2.603252 if district_code == 53
replace enep = 2.34864 if district_code == 54
replace enep = 2.681941 if district_code == 55
replace enep = 2.388509 if district_code == 56
replace enep = 2.324058 if district_code == 57
replace enep = 3.144311 if district_code == 58
replace enep = 3.186902 if district_code == 59
replace enep = 2.528328 if district_code == 60
replace enep = 2.616943 if district_code == 61
replace enep = 2.505764 if district_code == 62
replace enep = 2.810194 if district_code == 63
replace enep = 2.560075 if district_code == 64
replace enep = 2.721932 if district_code == 65
replace enep = 2.835207 if district_code == 66
replace enep = 2.900289 if district_code == 67
replace enep = 2.450604 if district_code == 68
replace enep = 2.276557 if district_code == 69
replace enep = 2.55968 if district_code == 70
replace enep = 2.839519 if district_code == 71
replace enep = 2.291416 if district_code == 72
replace enep = 2.138404 if district_code == 73
replace enep = 2.747776 if district_code == 74
replace enep = 2.528718 if district_code == 75
replace enep = 2.099749 if district_code == 76
replace enep = 2.82811 if district_code == 77
replace enep = 2.535643 if district_code == 78
replace enep = 2.654741 if district_code == 79
replace enep = 2.352741 if district_code == 80
replace enep = 2.596547 if district_code == 81
replace enep = 2.499609 if district_code == 82
replace enep = 2.495493 if district_code == 83
replace enep = 1.936559 if district_code == 84
replace enep = 2.339249 if district_code == 85
replace enep = 2.393587 if district_code == 86
replace enep = 1.793518 if district_code == 87
replace enep = 2.3218 if district_code == 88
replace enep = 2.360768 if district_code == 89
replace enep = 2.708393 if district_code == 90
replace enep = 2.888201 if district_code == 91
replace enep = 2.320981 if district_code == 92
replace enep = 2.76773 if district_code == 93
replace enep = 2.562163 if district_code == 94
replace enep = 2.63108 if district_code == 95
replace enep = 2.480072 if district_code == 96
replace enep = 2.439225 if district_code == 97
replace enep = 2.492702 if district_code == 98
replace enep = 2.220615 if district_code == 99
replace enep = 2.331071 if district_code == 100
replace enep = 2.535919 if district_code == 101
replace enep = 2.271726 if district_code == 102
replace enep = 1.922655 if district_code == 103


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if district_code == 1
replace reelected = 1 if district_code == 2
replace reelected = 1 if district_code == 3
replace reelected = 0 if district_code == 4
replace reelected = 0 if district_code == 5
replace reelected = 0 if district_code == 6
replace reelected = 1 if district_code == 7
replace reelected = 1 if district_code == 8
replace reelected = 1 if district_code == 9
replace reelected = 1 if district_code == 10
replace reelected = 1 if district_code == 11
replace reelected = 1 if district_code == 12
replace reelected = 1 if district_code == 13
replace reelected = 0 if district_code == 14
replace reelected = 1 if district_code == 15
replace reelected = 1 if district_code == 16
replace reelected = 1 if district_code == 17
replace reelected = 1 if district_code == 18
replace reelected = 1 if district_code == 19
replace reelected = 1 if district_code == 20
replace reelected = 0 if district_code == 21
replace reelected = 0 if district_code == 22
replace reelected = 0 if district_code == 23
replace reelected = 1 if district_code == 24
replace reelected = 0 if district_code == 25
replace reelected = 0 if district_code == 26
replace reelected = 1 if district_code == 27
replace reelected = 1 if district_code == 28
replace reelected = 1 if district_code == 29
replace reelected = 1 if district_code == 30
replace reelected = 0 if district_code == 31
replace reelected = 1 if district_code == 32
replace reelected = 0 if district_code == 33
replace reelected = 1 if district_code == 34
replace reelected = 1 if district_code == 35
replace reelected = 1 if district_code == 36
replace reelected = 0 if district_code == 37
replace reelected = 1 if district_code == 38
replace reelected = 0 if district_code == 39
replace reelected = 1 if district_code == 40
replace reelected = 1 if district_code == 41
replace reelected = 0 if district_code == 42
replace reelected = 0 if district_code == 43
replace reelected = 0 if district_code == 44
replace reelected = 0 if district_code == 45
replace reelected = 0 if district_code == 46
replace reelected = 0 if district_code == 47
replace reelected = 0 if district_code == 48
replace reelected = 0 if district_code == 49
replace reelected = 1 if district_code == 50
replace reelected = 1 if district_code == 51
replace reelected = 0 if district_code == 52
replace reelected = 1 if district_code == 53
replace reelected = 0 if district_code == 54
replace reelected = 0 if district_code == 55
replace reelected = 1 if district_code == 56
replace reelected = 0 if district_code == 57
replace reelected = 1 if district_code == 58
replace reelected = 1 if district_code == 59
replace reelected = 1 if district_code == 60
replace reelected = 1 if district_code == 61
replace reelected = 0 if district_code == 62
replace reelected = 1 if district_code == 63
replace reelected = 1 if district_code == 64
replace reelected = 1 if district_code == 65
replace reelected = 0 if district_code == 66
replace reelected = 0 if district_code == 67
replace reelected = 0 if district_code == 68
replace reelected = 1 if district_code == 69
replace reelected = 0 if district_code == 70
replace reelected = 1 if district_code == 71
replace reelected = 0 if district_code == 72
replace reelected = 1 if district_code == 73
replace reelected = 0 if district_code == 74
replace reelected = 0 if district_code == 75
replace reelected = 1 if district_code == 76
replace reelected = 0 if district_code == 77
replace reelected = 1 if district_code == 78
replace reelected = 1 if district_code == 79
replace reelected = 1 if district_code == 80
replace reelected = 1 if district_code == 81
replace reelected = 0 if district_code == 82
replace reelected = 1 if district_code == 83
replace reelected = 1 if district_code == 84
replace reelected = 0 if district_code == 85
replace reelected = 1 if district_code == 86
replace reelected = 1 if district_code == 87
replace reelected = 1 if district_code == 88
replace reelected = 1 if district_code == 89
replace reelected = 1 if district_code == 90
replace reelected = 1 if district_code == 91
replace reelected = 0 if district_code == 92
replace reelected = 1 if district_code == 93
replace reelected = 1 if district_code == 94
replace reelected = 0 if district_code == 95
replace reelected = 1 if district_code == 96
replace reelected = 0 if district_code == 97
replace reelected = 1 if district_code == 98
replace reelected = 1 if district_code == 99
replace reelected = 1 if district_code == 100
replace reelected = 1 if district_code == 101
replace reelected = 1 if district_code == 102
replace reelected = 1 if district_code == 103


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen time = 33 - (samp1 + 2)

* Gender

gen male = x11

recode male (2=0)

* Age

recode age 9999=.

* Education

gen education = x2

* Interest for election

gen interest = 6 - d1

* Party identification (provincial)

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if po1 == .

replace pidstatus_whole = 2 if po1 == 5

replace pidstatus_whole = 3 if po1 == 2 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if po1!=2 & po1!=. & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999
 
label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification scale (provincial)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & po2a == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & po2a == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & po2a == 3
replace pidscale_whole = 4 if pidstatus_whole == 2
replace pidscale_whole = 5 if pidstatus_whole == 3 & po2a == 3
replace pidscale_whole = 6 if pidstatus_whole == 3 & po2a == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & po2a == 1

label define pidscale 1 "Very strong loser PID" 2 "Fairly strong loser PID" 3 "Not very strong loser PID" 4 "No PID" 5 "Not very strong winner PID" 6 "Fairly strong winner PID" 7 "Very strong winner PID"
label values pidscale_whole pidscale

* Party identification (district)

gen pidstatus_district = .

replace pidstatus_district = 9999 if po1 == .

replace pidstatus_district = 2 if po1 == 5

replace pidstatus_district = 3 if po1 == district_outcome & pidstatus_district!=2 & pidstatus_district!=9999

replace pidstatus_district = 1 if po1!=district_outcome & po1!=. & pidstatus_district!=2 & pidstatus_district!=3 & pidstatus_district!=9999

replace pidstatus_district = . if pidstatus_district == 9999
 
label values pidstatus_district pidstatus

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & po2a == 1
replace pidscale_district = 2 if pidstatus_district == 1 & po2a == 2
replace pidscale_district = 3 if pidstatus_district == 1 & po2a == 3
replace pidscale_district = 4 if pidstatus_district == 2
replace pidscale_district = 5 if pidstatus_district == 3 & po2a == 3
replace pidscale_district = 6 if pidstatus_district == 3 & po2a == 2
replace pidscale_district = 7 if pidstatus_district == 3 & po2a == 1

label values pidscale_district pidscale

* TV news attentiveness

gen tv = ma2

recode tv (.=-99)

* Press news attentiveness

gen press = ma1

recode press (.=-99)

* News attentiveness index

egen news = rowmax(tv press)

recode news (-99=.)

* Caring about outcome

gen care = p9

recode care (2=0)

* Name of the leader of the provincial Progressive Conservatives - Correct answer (Ernie Eves) = 1, Incorrect answer = 0

gen opcleader = c1 // OPC leader

recode opcleader (2=0)
recode opcleader (3=0)

* Name of the leader of the provincial Liberal Party - Correct answer (Dalton McGuinty) = 1, Incorrect answer = 0

gen olpleader = c2 // OLP leader

recode olpleader (2=0)

* Name of the leader of the provincial New Democratic Party - Correct answer (Howard Hampton) = 1, Incorrect answer = 0

gen ondpleader = c3 // ONDP leader

recode ondpleader (2=0)

* Name of the last NDP premier - Correct answer (Bob Rae) = 1, Incorrect answer = 0

gen ondppremier = c4 // Last NDP premier

recode ondpleader (2=0)

* Name of the minister of Finance of Canada

gen financeminister = c5

recode financeminister (2=0)

* Subject of Roy Romanow Royal Commission

gen commission = c6

recode commission (2=0)

* Political knowledge index

gen knowledge = opcleader + olpleader + ondpleader + ondppremier + financeminister + commission // Political knowledge index

* Political discussion

gen discussion = 5 - d2

gen discussion_3pts = discussion - 1
replace discussion_3pts = 0 if discussion_3pts == 1
replace discussion_3pts = 1 if discussion_3pts == 2
replace discussion_3pts = 2 if discussion_3pts == 3


***********************************
** PROVINCIAL-LEVEL EXPECTATIONS **
***********************************

* Forecasts

gen forecast_whole = f4

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Generate variable identifying incomplete answers

gen olp_chance_district = f1
gen opc_chance_district = f2
gen ondp_chance_district = f3

gen complete_district = 1 if olp_chance_district!=. & opc_chance_district!=. & ondp_chance_district!=.

* Generate value of 1 if the expectation for one candidate is higher or equal to the expectations for at least one other candidate

gen olp_district = 1 if olp_chance_district >= opc_chance_district & olp_chance_district >= ondp_chance_district & complete_district == 1
gen opc_district = 1 if opc_chance_district >= olp_chance_district & opc_chance_district >= ondp_chance_district & complete_district == 1
gen ondp_district = 1 if ondp_chance_district >= olp_chance_district & ondp_chance_district >= opc_chance_district & complete_district == 1
gen oth_district = 1 if olp_chance_district == 0 & opc_chance_district == 0 & ondp_chance_district == 0

replace olp_district = . if oth_district == 1 & complete_district == 1
replace opc_district = . if oth_district == 1 & complete_district == 1
replace ondp_district = . if oth_district == 1 & complete_district == 1

* Generate variable for ambiguous forecasts

gen ambiguous_district = .

replace ambiguous_district = 99 if olp_district == opc_district & olp_district!=. & complete_district == 1 | olp_district == ondp_district & olp_district!=. & complete_district == 1
replace ambiguous_district = 99 if opc_district == olp_district & opc_district!=. & complete_district == 1 | opc_district == ondp_district & opc_district!=. & complete_district == 1
replace ambiguous_district = 99 if ondp_district == olp_district & ondp_district!=. & complete_district == 1 | ondp_district == opc_district & ondp_district!=. & complete_district == 1

* Generate value of 0 for lower expectations

replace olp_district = 0 if olp_district!=1 & complete_district == 1
replace opc_district = 0 if opc_district!=1 & complete_district == 1
replace ondp_district = 0 if ondp_district!=1 & complete_district == 1
replace oth_district = 0 if oth_district!=1 & complete_district == 1

* Forecasts (1 = OLP, 2 = OPC, 3 = ONDP, 88 = other, 99 = ambiguous)

gen forecast_district = .

replace forecast_district = 1 if olp_district == 1 & complete_district == 1
replace forecast_district = 2 if opc_district == 1 & complete_district == 1
replace forecast_district = 3 if ondp_district == 1 & complete_district == 1
replace forecast_district = 88 if oth_district == 1 & complete_district == 1
replace forecast_district = 99 if ambiguous_district == 99 & complete_district == 1

* Identify correct and ambiguous forecasts

gen correct_district = 0 if forecast_district!=district_outcome & complete_district == 1
replace correct_district = 1 if forecast_district == district_outcome & forecast_district!=99 & complete_district == 1

replace correct_district = 0 if forecast_district == 99 & olp_district!=1 & district_outcome == 2 & complete_district == 1
replace correct_district = 0 if forecast_district == 99 & opc_district!=1 & district_outcome == 1 & complete_district == 1
replace correct_district = 0 if forecast_district == 99 & ondp_district!=1 & district_outcome == 3 & complete_district == 1
replace correct_district = 0 if forecast_district == 99 & oth_district!=1 & district_outcome == 88 & complete_district == 1

replace correct_district = 99 if forecast_district == 99 & opc_district == 1 & district_outcome == 1 & complete_district == 1
replace correct_district = 99 if forecast_district == 99 & olp_district == 1 & district_outcome == 2 & complete_district == 1
replace correct_district = 99 if forecast_district == 99 & ondp_district == 1 & district_outcome == 3 & complete_district == 1
replace correct_district = 99 if forecast_district == 99 & oth_district == 1 & district_outcome == 88 & complete_district == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correct_district_d = correct_district

replace correct_district_d = 0 if correct_district_d == 99
replace correct_district_d = 1 if correct_district_d == 1


**********
** SAVE **
**********

save "on2003_oes.dta", replace


********************************
** MAIN ANALYSES (PROVINCIAL) **
********************************

* Regression analysis and percentage of correct and incorrect forecasts (provincial)

logistic correct_whole_d discussion pidscale_whole interest i.care news i.male age education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


*************************************
** SUMMARY STATISTICS (PROVINCIAL) **
*************************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable pidscale_whole "Provincial PID"
label variable pidscale_district "District PID"
label variable discussion "Discussion"
label variable interest "Interest"
label variable care "Caring about outcome"
label variable news "News attentiveness"
label variable male "Gender (male = 1)"
label variable age "Age"
label variable education "Education"
label variable margin "Margin"
label variable reelected "Reelected"
label variable enep "ENEP"
label variable time "Time of interview"

estpost tabstat correct_whole_d discussion pidscale_whole interest care news male age education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "on2003_oes_summary_provincial.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2003 Ontario provincial election (OES), provincial level") replace

eststo clear


******************************
** MAIN ANALYSES (DISTRICT) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion pidscale_district interest news i.male age education time enep i.reelected || district_code: , or
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (DISTRICT) **
***********************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable pidscale_district "District PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable male "Gender (male = 1)"
label variable age "Age"
label variable education "Education"
label variable margin "Margin"
label variable reelected "Reelected"
label variable enep "ENEP"
label variable time "Time of interview"

estpost tabstat correct_district_d discussion pidscale_district interest news male age education time enep reelected if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "on2003_oes_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2003 Ontario provincial election (OES), district level") replace

eststo clear

log close